//
//  SJPDepartmentViewController.m
//  SWS_Project00
//
//  Created by 马继鵬 on 17/3/8.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "SJPDepartmentViewController.h"
#import "SJPDepatmentModel.h"
#import "SJPBaseTableViewController.h"
#import "SJPDepartmentTableViewController.h"
#import "SJPAppenedTableController.h"
#import "SJPCureMethodViewController.h"
#import "SJPDepartControl.h"


@interface SJPDepartmentViewController ()
//建立特殊的control
@property (weak,nonatomic)  SJPDepartControl* departmentControl;
///按键缓存池
@property (strong,nonatomic) NSCache* btnCache;
//确诊文本框内容
@property (strong,nonatomic) NSCache* certainCache;
@property (weak,nonatomic) UIView* cureView;

//观察者相关参数
@property (strong,nonatomic)  UIButton *keyBtn;
//要进行复用所以用强引用
@property (strong,nonatomic)  SJPDepartControl* cureMethodView;
@end

@implementation SJPDepartmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置颜色
    self.view.backgroundColor = [UIColor colorWithRed:235.0/255 green:235.0/255 blue:241.0/255 alpha:1.0];
    _btnCache = [NSCache new];
    _certainCache = [NSCache new];
    //设置UI
    [self setupUI];
}

- (void)setupUI{
    //添加用户信息
    UILabel* userNameLabel = [UILabel EPM_labelWithTitle:@"用户信息: 1000099" textColor:[UIColor blackColor] font:20];
    [self.view addSubview:userNameLabel];
    [userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.offset(21);
    }];
    //添加科室信息
    UILabel* departmentLabel = [UILabel EPM_labelWithTitle:nil textColor:[UIColor blackColor] font:20];
    [self.view addSubview:departmentLabel];
    
    departmentLabel.text = [NSString stringWithFormat:@"疾病类型:  %@",self.model.title];
    [departmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.equalTo(userNameLabel.mas_bottom).offset(10);
    }];
    //添加疾病分类Control
     SJPDepartControl* departmentControl = [SJPDepartControl departmentControlWithPlaceHolder:@"请选择疾病明细" title:nil ID:0];
    [self.view addSubview:departmentControl];
    _departmentControl = departmentControl;
    
    [departmentControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.equalTo(departmentLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(self.view.width-34);
    }];
   
    //添加点击事件
    [departmentControl addTarget:self action:@selector(jumptoSelection:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加并发症Control
    SJPDepartControl* appendControl = [SJPDepartControl departmentControlWithPlaceHolder:@"请选择并发症(可多选)" title:nil ID:2];
   
    [self.view addSubview:appendControl];
   
 
    [appendControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.equalTo(departmentControl.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.trailing.offset(-17);
    }];
    
    [appendControl addTarget:self action:@selector(jumptoSelection:) forControlEvents:UIControlEventTouchUpInside];
    
    //确诊View
    UIView* certainView = [self selectionViewWithCertainTitle:@"已确诊" uncertainTitle:@"症状疑似" placeHolder:@"是否确诊" ID:0];
    [self.view addSubview:certainView];
    //位置设置
    [certainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.equalTo(appendControl.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.trailing.offset(-17);
    }];
    
    //治疗方式View
    UIView* cureView = [self selectionViewWithCertainTitle:@"接受过" uncertainTitle:@"未接受过" placeHolder:@"是否接受过治疗" ID:2];
     _cureView = cureView;
   
    [self.view addSubview:cureView];
    [cureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.equalTo(certainView.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.trailing.offset(-17);
    }];
   
    
    
#pragma
#pragma- 设置底部视图

    //设置底部按键
    UIButton* bottomBtn = [[UIButton alloc]init];
    [self.view addSubview:bottomBtn];
    [bottomBtn setTitle:@"就医申请" forState:UIControlStateNormal];
    [bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bottomBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [bottomBtn setBackgroundColor:kThemeColor];
    bottomBtn.layer.cornerRadius = 5;
    bottomBtn.clipsToBounds = YES;
    //设置位置
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.trailing.offset(-10);
        make.height.mas_equalTo(40);
    }];
    
    //设置底部图片
    UIImageView* bottomImageView = [[UIImageView alloc]init];
    [self.view addSubview:bottomImageView];
    bottomImageView.image = [UIImage imageNamed:@"illness_img_person.png"];
    [bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottomBtn.mas_top).offset(-30);
        make.centerX.offset(0);
        make.width.height.mas_equalTo(90);
    }];
    
    //设置Label
    UILabel *bottomLabel = [UILabel EPM_labelWithTitle:@"已为你匹配到: 42位医生" textColor:[UIColor blackColor] font:20];
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottomImageView.mas_top).offset(-30);
        make.centerX.offset(0);
    }];
    
    
}
#pragma
#pragma-确诊View
- (UIView*)selectionViewWithCertainTitle:(NSString*)certainTitle uncertainTitle:(NSString*)unclearTitle placeHolder:(NSString*)placeHolder ID:(NSInteger)ID
{
    UIView* view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    UITextField* textField = [[UITextField alloc]init];
    [view addSubview:textField];
    textField.textColor = [UIColor blackColor];
    textField.enabled = NO;
    textField.font = [UIFont systemFontOfSize:20];
    textField.placeholder = placeHolder;
    textField.tag = ID;
    [_certainCache setObject:textField forKey:@(textField.tag)];
    //位置
     [textField mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.offset(0);
         make.leading.offset(10);
         make.width.mas_equalTo(self.view.width*0.4);
     }];
    //添加确认选择
    UIButton* certainBtn = [[UIButton alloc]init];
    certainBtn.tag = ID;
    [_btnCache setObject:certainBtn forKey:@(certainBtn.tag)];
    [certainBtn setTitle:certainTitle forState:UIControlStateNormal];
    [certainBtn setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [certainBtn setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [certainBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [view addSubview:certainBtn];
    [certainBtn addTarget:self action:@selector(checkClick:) forControlEvents:UIControlEventTouchUpInside];
    certainBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    //位置
    [certainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(textField.mas_trailing);
        make.centerY.offset(0);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
    }];
    //添加未确认按钮
    UIButton* uncertainBtn = [[UIButton alloc]init];
    uncertainBtn.tag = ID+1;
    [_btnCache setObject:uncertainBtn forKey:@(uncertainBtn.tag)];
    [uncertainBtn setTitle:unclearTitle forState:UIControlStateNormal];
    [uncertainBtn setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [uncertainBtn setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
   [uncertainBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [view addSubview:uncertainBtn];
    [uncertainBtn addTarget:self action:@selector(checkClick:) forControlEvents:UIControlEventTouchUpInside];
    uncertainBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    //布局
    [uncertainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.offset(10);
        make.centerY.offset(0);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];
    return view;
}
#pragma
#pragma-点击方法实现
- (void)checkClick:(UIButton*)sender
{
    NSInteger ID = sender.tag;
    sender.selected = YES;
    switch (ID) {
        case 0:{
            UIButton* btn = [self.btnCache objectForKey:@(1)];
            btn.selected = NO;
            UITextField* textField = [self.certainCache objectForKey:@(0)];
            textField.text = sender.titleLabel.text;
        }
            break;
        case 1:{
            UIButton* btn = [self.btnCache objectForKey:@(0)];
            btn.selected = NO;
            UITextField* textField = [self.certainCache objectForKey:@(0)];
            textField.text = sender.titleLabel.text;
        }
            break;
        case 2:{
            UIButton* keyBtn = [self.btnCache objectForKey:@(2)];
            _keyBtn = keyBtn;
            UIButton* btn = [self.btnCache objectForKey:@(3)];
            btn.selected = NO;
            UITextField* textField = [self.certainCache objectForKey:@(2)];
            textField.text = sender.titleLabel.text;
            [self setupCureMethodView];
            //添加监听
            [keyBtn addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
        }
            break;
        case 3:{
            UIButton* btn = [self.btnCache objectForKey:@(2)];
            btn.selected = NO;
            UITextField* textField = [self.certainCache objectForKey:@(2)];
            textField.text = sender.titleLabel.text;
            
        }
        default:
            break;
    }

}
#pragma
#pragma-调用观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"selected"])
    {
       
        if ( _keyBtn.selected != YES )
        {
            [_cureMethodView  removeFromSuperview];
        }
    }
}

#pragma
#pragma-治疗方式View
- (void)setupCureMethodView
{
    //当_cureMethod不存在时调用方法生成,防止重复生成
    if (_cureMethodView == nil){
        SJPDepartControl* cureMethodView = [SJPDepartControl  departmentControlWithPlaceHolder:@"请选择治疗方式" title:nil ID:3];
        _cureMethodView = cureMethodView;
    }
    
    [self.view addSubview:_cureMethodView];
    //位置
    [_cureMethodView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(17);
        make.top.equalTo(_cureView.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.trailing.offset(-17);
    }];
    [_cureMethodView addTarget:self action:@selector(jumptoSelection:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma
#pragma-点击事件
- (void)jumptoSelection:(SJPDepartControl*)sender
{
    
    
    if (sender.tag != 0 && _departmentControl.title.length == 0)
        return;
    
    SJPBaseTableViewController* tabController;
    switch (sender.tag) {
        case 0:
        {
            tabController = [SJPDepartmentTableViewController new];
//            tabController.title = @"1";
            [self.navigationController pushViewController:tabController animated:YES];
            tabController.titleBlock = ^(NSString* title){
                sender.title = title;
            };
           
        }
            break;
        case 2:
        {
            tabController = [SJPAppenedTableController new];
            tabController.title = @"并发症";
            [self.navigationController pushViewController:tabController animated:YES];
            tabController.titleBlock = ^(NSString* title){
                sender.title = title;
            };
        }
            break;
        case 3:
        {
            tabController = [SJPCureMethodViewController new];
            tabController.title = @"治疗方式";
            [self.navigationController pushViewController:tabController animated:YES];
            tabController.titleBlock = ^(NSString* title){
                sender.title = title;
            };
        }
            break;
        default:
            break;
    }
    
    
}


- (void)dealloc
{
//    NSLog(@"%s",__func__);
   
    [_keyBtn removeObserver:self forKeyPath:@"selected"];
}
@end
